package com.ym.livedemo.live1

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.ym.livedemo.*
import com.ym.livedemo.databinding.ActivityAnchorLiveBinding
import me.lake.librestreaming.core.listener.RESConnectionListener
import me.lake.librestreaming.filter.hardvideofilter.SkinBlurHardVideoFilter
import me.lake.librestreaming.filter.hardvideofilter.WhiteningHardVideoFilter
import me.lake.librestreaming.ws.StreamAVOption
import me.lake.librestreaming.ws.StreamLiveCameraView
import splitties.toast.UnreliableToastApi
import splitties.toast.toast

class AnchorLiveActivity : AppCompatActivity() {
    private val TAG = "AnchorLiveActivity"
    private lateinit var streamAVOption: StreamAVOption
    private lateinit var streamLiveCameraView: StreamLiveCameraView
    private lateinit var viewBinding: ActivityAnchorLiveBinding
    private var rtmpUrl = ""
    private var skinBlur = 1

    companion object {
        const val RTMP_URL = "RTMP_URL"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ActivityAnchorLiveBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)
        SystemBarHelper.immersiveStatusBar(this, 0f)
        rtmpUrl = intent.getStringExtra(RTMP_URL).toString()
        initLive()
    }

    private fun initLive() {
        streamLiveCameraView = viewBinding.streamPreviewView
        streamAVOption = StreamAVOption()
        streamAVOption.videoBitrate = Constant.VIDEO_BITRATE
        streamAVOption.videoFramerate = Constant.VIDEO_FPS
        streamAVOption.streamUrl = rtmpUrl
        streamLiveCameraView.init(this, streamAVOption)
        streamLiveCameraView.setHardVideoFilter(SkinBlurHardVideoFilter(skinBlur))
        viewBinding.btnRefresh.setOnClickListener {
            streamLiveCameraView.refreshPreviewTextureView()
        }
        viewBinding.btnReset.setOnClickListener {
            streamLiveCameraView.resetResolution(1080, 1920)
        }
        viewBinding.btnSkinBlur.setOnClickListener {
            skinBlur++
            viewBinding.btnSkinBlur.text = "磨皮: $skinBlur"
            streamLiveCameraView.setHardVideoFilter(SkinBlurHardVideoFilter(skinBlur))
        }
        viewBinding.btnWhiteHard.setOnClickListener {
            streamLiveCameraView.setHardVideoFilter(WhiteningHardVideoFilter())
        }
        streamLiveCameraView.addStreamStateListener(resConnectionListener)

        viewBinding.btnStartStreaming.setOnClickListener {
            loge("开始推流: $rtmpUrl")
            streamLiveCameraView.startStreaming(rtmpUrl)
            refreshLiveStatus()
        }
        viewBinding.btnStopStreaming.setOnClickListener {
            streamLiveCameraView.stopStreaming()
            refreshLiveStatus()
            finish()
        }

        viewBinding.btnSwapCamera.setOnClickListener {
            streamLiveCameraView.swapCamera()
        }
    }

    private fun refreshLiveStatus() {
        if (streamLiveCameraView.isStreaming) {
            viewBinding.btnStartStreaming.makeGone()
            viewBinding.btnStopStreaming.makeVisible()
        } else {
            viewBinding.btnStartStreaming.makeVisible()
            viewBinding.btnStopStreaming.makeGone()
        }
    }

    @OptIn(UnreliableToastApi::class)
    var resConnectionListener: RESConnectionListener = object :
        RESConnectionListener {
        override fun onOpenConnectionResult(result: Int) {
            //result 0成功  1 失败
            toast("打开推流连接 状态：${if (result == 1) "失败" else "成功"}")
            if (result == 1) {
//                finish()
            }
        }

        override fun onWriteError(errno: Int) {
            toast("推流出错,请尝试重连")
//            finish()
        }

        override fun onCloseConnectionResult(result: Int) {
            //result 0成功  1 失败
            toast("关闭推流连接 状态：$result")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        streamLiveCameraView.destroy()
    }

}